<script setup>
import {
  Edit,
  Delete,
  Close
} from '@element-plus/icons-vue'


import {ref} from 'vue'


const contentTypes = ref([])


const lotterys = ref([])

const type = ref([])



const resourceTypes = ref([
  {
    "id": 1,
    "resourceValue": "星星",
  },
  {
    "id": 2,
    "resourceValue": "矿石",
  } ,
  {
    "id": 3,
    "resourceValue": "头像",
  },
  {
    "id": 7,
    "resourceValue": "表情包",
  },

  {
    "id": 8,
    "resourceValue": "谢谢惠顾",
  }

])

const types = ref([
  {
    "id": 0,
    "resourceValue": "外星人",
  },
  {
    "id": 1,
    "resourceValue": "小狗",
  }
])


//用户搜索时选中的分类id
const resourceType = ref('')

const account = ref('')

//用户搜索时选中的发布状态
const state = ref('')


const lotteryId = ref('')

const resourceList = ref([])


//分页条数据模型
const pageNum = ref(1)//当前页
const total = ref(20)//总条数
const pageSize = ref(15)//每页条数

//当每页条数发生了变化，调用此函数
const onSizeChange = (size) => {
  pageSize.value = size
  dataList()
}
//当前页码发生变化，调用此函数
const onCurrentChange = (num) => {
  pageNum.value = num
  dataList()
}


import {
  informationDeleteService,
  resourceInformationAddService,
  resourceInformationEditService,
  resourceInformationListService
} from '@/api/resourceInformation.js'


import {Plus} from '@element-plus/icons-vue'
import {QuillEditor} from '@vueup/vue-quill'
import '@vueup/vue-quill/dist/vue-quill.snow.css'
//控制抽屉是否显示
const visibleDrawer = ref(false)
//添加表单数据模型
const dataModel = ref({})


//导入token
import {useTokenStore} from '@/stores/token.js';

const tokenStore = useTokenStore();


//上传成功的回调函数
const uploadBackgroundUrlSuccess = (result) => {
  dataModel.value.backgroundUrl = result.data.fileUrl;
}

const uploadResourceSuccess = (result) => {
  dataModel.value.fileUrl = result.data.fileUrl;
  dataModel.value.fileName = result.data.fileName;
}

//添加文章
import {ElMessage} from 'element-plus'

const addData = async (clickState) => {

  //调用接口
  let result = await homeMessageResourceAddService(dataModel.value);

  ElMessage.success(result.msg ? result.msg : '添加成功');

  //让抽屉消失
  visibleDrawer.value = false;

  //刷新当前列表
  dataList()
}

//编辑分类
const editData = async () => {
  dataModel.value.fileUrlList =fileList;
  //调用接口
  let result = await homeMessageResourceEditService(dataModel.value);

  ElMessage.success(result.msg ? result.msg : '修改成功')

  //调用获取所有分类的函数
  dataList();

  //隐藏弹窗
  visibleDrawer.value = false;
}


//获取文章列表数据
const dataList = async () => {
  let params = {
    pageNum: pageNum.value,
    pageSize: pageSize.value,
    type: type.value!=='' ? type.value : null
  }
  let result = await homeMessageResourceListService(params);

  total.value = result.data.total;
  resourceList.value = result.data.records;

}



dataList();



//删除分类
import {ElMessageBox} from 'element-plus'
import {knowledgeDeleteService} from "@/api/knowledge.js";
import {
  lotteryListService,
  lotteryResourceAddService,
  lotteryRewardListService,
  lotteryRewardDeleteService, lotteryRewardEditService
} from "@/api/lottery.js";
import {versionSeasonListService} from "@/api/versionSeason.js";
import {
  worldMessageResourceAddService, worldMessageResourceDelService,
  worldMessageResourceEditService,
  worldMessageResourceListService, worldMessageResourcePublishService
} from "@/api/worldMessage.js";
import {interestPublishInterestPublishService} from "@/api/interestPublishRecordData.js";
import {
  homeMessageResourceAddService, homeMessageResourceDelService,
  homeMessageResourceEditService,
  homeMessageResourceListService, homeMessageResourcePublishService
} from "@/api/messageResource.js";

const deleteData = (row) => {
  //提示用户  确认框

  ElMessageBox.confirm(
      '你确认要删除该话术管理信息吗?',
      '温馨提示',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(async () => {
        //调用接口
        let result = await homeMessageResourceDelService(row.id);
        ElMessage({
          type: 'success',
          message: '删除成功',
        })
        //刷新列表
        dataList();
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '用户取消了删除',
        })
      })
}


//定义变量,控制标题的展示
const title = ref('')
const showData = (row) => {
  visibleDrawer.value = true, title.value = '编辑话术管理'
  dataModel.value.id= row.id;
  dataModel.value.type=row.type;
  dataModel.value.content=row.content;


}

//清空模型的数据
const clearData = () => {
  dataModel.value.id='';
  dataModel.value.content='';
  dataModel.value.type='';
}
const handleSelectionChange = (val) => {
  select = val;
}

const selected = (row, index) => {
  return resourceList.value[index].publishState == 0
}
const delFileUrl = () => {
  dataModel.value.fileUrl = '';
  dataModel.value.fileName = '';
}

const delBackgroundUrl = () => {
  dataModel.value.backgroundUrl = '';
}

const fileList = ref([])

let select = []
const publish = async () => {

  if (select.length === 0) {
    ElMessage.error("请选择要发布的资源内容")
    return
  }
  let ids = []
  select.forEach(element => {
    ids.push(element.id)
  });
  let params = {
    ids: ids
  }
  let result = await homeMessageResourcePublishService(params);

  dataList();

}

</script>
<template>
  <el-card class="page-container">
    <template #header>
      <div class="header">
        <span>话术管理</span>
        <div class="extra">
          <el-button type="primary" @click="visibleDrawer = true; title = '添加话术管理'; clearData()">添加话术管理
          </el-button>
        </div>
      </div>
    </template>
    <!-- 搜索表单 -->

    <el-form inline>
      <el-form-item label="选择类型：">
        <el-select placeholder="请选择" v-model="type">
          <el-option v-for="c in types" :key="c.id" :label="c.resourceValue" :value="c.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="dataList">搜索</el-button>
        <el-button @click="type = ''">重置</el-button>
      </el-form-item>
    </el-form>

    <el-form inline>
      <el-button type="primary" @click="publish">发布</el-button>
    </el-form>
    <!-- 文章列表 -->
    <el-table :data="resourceList" @selection-change="handleSelectionChange" border style="width: 100%">
      <el-table-column type="selection" width="55" :selectable="selected"/>
      <el-table-column label="类型" >
        <template v-slot="scope">
          <span v-if="scope.row.type==0">外星人</span>
          <span v-if="scope.row.type==1">小狗</span>

          <span v-else ></span>
        </template>
      </el-table-column>

      <el-table-column label="内容" prop="content"></el-table-column>

      <el-table-column label="发布状态" >
        <template v-slot="scope">
          <span v-if="scope.row.publishState==0">未发布</span>
          <span v-if="scope.row.publishState==1">已发布</span>

          <span v-else ></span>
        </template>
      </el-table-column>



      <el-table-column label="创建时间" prop="createTime" width="200"></el-table-column>

      <el-table-column label="操作" >
        <template #default="{ row }">
          <el-button :icon="Edit" circle plain type="primary" @click="showData(row)"></el-button>

          <el-button :icon="Delete" circle plain type="danger" @click="deleteData(row)"></el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据"/>
      </template>
    </el-table>


    <!-- 分页条 -->
    <el-pagination v-model:current-page="pageNum" v-model:page-size="pageSize" :page-sizes="[3, 5, 10, 15]"
                   layout="jumper, total, sizes, prev, pager, next" background :total="total"
                   @size-change="onSizeChange"
                   @current-change="onCurrentChange" style="margin-top: 20px; justify-content: flex-end"/>


    <!-- 抽屉 -->
    <el-drawer v-model="visibleDrawer" :title="title" direction="rtl" size="50%">
      <el-form :model="dataModel" label-width="200px">

        <el-form-item label="选择类型：">
          <el-select placeholder="请选择" v-model="dataModel.type">
            <el-option v-for="c in types" :key="c.id" :label="c.resourceValue" :value="c.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="消息内容" >
          <el-input v-model="dataModel.content" type="textarea" style="width: 500px"/>
        </el-form-item>








        <el-form-item>
          <el-button type="primary" @click="title == '添加话术管理' ? addData() : editData()">保存</el-button>

        </el-form-item>
      </el-form>
    </el-drawer>


  </el-card>
</template>
<style lang="scss" scoped>
.page-container {
  min-height: 100%;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }
}


/* 抽屉样式 */
.avatar-uploader {
  :deep() {
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }

    .el-upload {
      border: 1px dashed var(--el-border-color);
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
      transition: var(--el-transition-duration-fast);
    }

    .el-upload:hover {
      border-color: var(--el-color-primary);
    }

    .el-icon.avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      text-align: center;
    }
  }
}

.editor {
  width: 100%;

  :deep(.ql-editor) {
    min-height: 200px;
  }
}
</style>